package com.simon.recyclerview;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.simon.recyclerview.adapter.TestAdapter;
import com.simon.recyclerview.utils.ToastHelper;
import com.simon.refreshlibrary.PtrClassicFrameLayout;
import com.simon.refreshlibrary.PtrDefaultHandler;
import com.simon.refreshlibrary.PtrFrameLayout;
import com.simon.refreshlibrary.PtrHandler;
import com.simon.refreshlibrary.recyclerview.adapter.UniversalAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by: Simon
 * Email: simon.han0220@gmail.com
 * Created on: 2016/9/28 0028 11:41
 */

public class RefreshActivity extends AppCompatActivity implements UniversalAdapter.LoadMoreListener {
    private PtrClassicFrameLayout mPtrFrame;
    private TestAdapter mAdapter;

    private static final int TOTAL_COUNTER = 50;

    private int delayMillis = 2000;

    private int mCurrentCounter = 0;
    private boolean isErr;
    private RecyclerView mRecyclerView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_refresh);
//        mPtrFrame = (PtrClassicFrameLayout) findViewById(R.id.rotate_header_list_view_frame);
        mPtrFrame = (PtrClassicFrameLayout) findViewById(R.id.rotate_header_list_view_frame);
        mPtrFrame.setLastUpdateTimeRelateObject(this);

        mRecyclerView = (RecyclerView) findViewById(R.id.rotate_header_list_view);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

        List<String> data = createData(10);
        mAdapter = new TestAdapter(data);
        mAdapter.setLoadMoreEnable(true);
        mAdapter.setOnLoadMoreListener(this);
        mRecyclerView.setAdapter(mAdapter);

        View headerView = getView(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mAdapter.addHeaderView(getView(getRemoveHeaderListener(), "click me to remove " +
                        "me"), 0);
            }
        }, "click me to add new header");
        mAdapter.addHeaderView(headerView);

//        View footerView = getView(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                mAdapter.addFooterView(getView(getRemoveFooterListener(), "click me to remove me"));
//            }
//        }, "click me to add new footer");
//        mAdapter.addFooterView(footerView, 0);

        mPtrFrame.setPtrHandler(new PtrHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                mAdapter.resetState();
                mCurrentCounter = 10;
                final List<String> data1 = createData(10);
                mPtrFrame.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (null != mAdapter) {
//                            mAdapter.getData().clear();
//                            mAdapter.getData().addAll(data1);
//                            mAdapter.notifyDataSetChanged();
                            mAdapter.setNewData(data1);
                        }
                        mPtrFrame.refreshComplete();
                    }
                }, 2000);

            }

            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header);
            }
        });
        // the following are default settings
        mPtrFrame.setResistance(2.0f);
        mPtrFrame.setRatioOfHeaderHeightToRefresh(1.2f);
        mPtrFrame.setDurationToClose(200);
        mPtrFrame.setDurationToCloseHeader(500);
        // default is false
        mPtrFrame.setPullToRefresh(false);
        // default is true
        mPtrFrame.setKeepHeaderWhenRefresh(true);
        mPtrFrame.postDelayed(new Runnable() {
            @Override
            public void run() {
                mPtrFrame.autoRefresh();
            }
        }, 100);

    }


    public List<String> createData(int count) {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            list.add("RecyclerView Item " + i);
        }
        return list;
    }

    @Override
    public void onLoadMore() {
        mRecyclerView.post(new Runnable() {
            @Override
            public void run() {
                if (mCurrentCounter >= TOTAL_COUNTER) {

                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            mAdapter.showNOMoreView();
                        }
                    }, delayMillis);

                } else {
                    if (isErr) {
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                mAdapter.loadComplete();
                                mAdapter.appendData(createData(10));
                                mCurrentCounter = mAdapter.getData().size();
                            }
                        }, delayMillis);
                    } else {
                        isErr = true;
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                ToastHelper.longToast("请求失败");
                                mAdapter.showLoadMoreFailedView();
                            }
                        }, delayMillis);


                    }
                }
            }

        });
    }

    private View.OnClickListener getRemoveHeaderListener() {
        return new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mAdapter.removeHeaderView(v);
            }
        };
    }

    private View.OnClickListener getRemoveFooterListener() {
        return new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mAdapter.removeFooterView(v);
            }
        };
    }

    private View getView(View.OnClickListener listener, String text) {
        View view = getLayoutInflater().inflate(R.layout.head_view, null);
        view.setLayoutParams(new DrawerLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));
        ((TextView) view.findViewById(R.id.tv)).setText(text);
        view.setOnClickListener(listener);
        return view;
    }
}
